#!/usr/bin/perl

use strict;
use warnings;

#open LIST, "tasks.txt";
open LIST, "tasks_100714.txt";

my @aas = ('ALA', 'CYS','ASP','GLU','PHE','GLY','HIS','ILE','LYS','LEU','MET','ASN','PRO',"GLN",'ARG','SER','THR','VAL','TRP','TYR');

my %helixhisto;
my %helixhshisto;
my %ddgs;
my %hsddgs;
my %cchisto;
my %ccddgs;
while (<LIST>) {
	# each item is a standard format line that tells us what we need to examine
	my $line = $_;
	my @objs = split (" ", $line);
	my $pdb = $objs[0];
	my $mode = $objs[1];
	my $ch1 = $objs[2];
	my $start1 = $objs[3];
	my $end1 = $objs[4];
	my $ch2 = $objs[5];
	my $start2 = $objs[6];
	my $end2 = $objs[7];
	my $ptnr = $objs[13];

	open IN1, "procced_alaout/$pdb\_$ch1\_$ptnr\_ala_out_prepped.txt";
	open IN2, "procced_alaout/$pdb\_$ch2\_$ptnr\_ala_out_prepped.txt";
	while (<IN1>) {
		my @inobjs = split;
		if ($inobjs[2] eq $ch1) {
			if ($inobjs[1] >= $start1 and $inobjs[1] <= $end1) {
				$helixhisto{$inobjs[0]}++;									 
				$ddgs{$inobjs[0]} += $inobjs[4];
				if ($inobjs[4] > 1) {
					$hsddgs{$inobjs[0]} += $inobjs[4];
					$helixhshisto{$inobjs[0]}++;									 
				}	
			}
		}
	}									 
	while (<IN2>) {
		my @inobjs = split;
		if ($inobjs[2] eq $ch2) {
			if ($inobjs[1] >= $start1 and $inobjs[1] <= $end1) {
				$helixhisto{$inobjs[0]}++;									 
				$ddgs{$inobjs[0]} += $inobjs[4];
				if ($inobjs[4] > 1) {
					$hsddgs{$inobjs[0]} += $inobjs[4];
					$helixhshisto{$inobjs[0]}++;									 
				}	
			}
		}
	}	
	close IN2;
	close IN1;								 

	if ($ch1 ne $ch2) {
		open CC, "procced_alaout/$pdb\_$ch1\_$ch2\_ala_out_prepped.txt" or open CC, "procced_alaout/$pdb\_$ch2\_$ch1\_ala_out_prepped.txt";
		while (<CC>) {
			my @inobjs = split;
			if ($inobjs[2] eq $ch1) {
				if ($inobjs[1] >= $start1 and $inobjs[1] <= $end1) {
					$cchisto{$inobjs[0]}++;									 
					$ccddgs{$inobjs[0]} += $inobjs[4];
				}
			}
			if($inobjs[2] eq $ch2) {
				if ($inobjs[1] >= $start2 and $inobjs[1] <= $end2) {
					$cchisto{$inobjs[0]}++;									 
					$ccddgs{$inobjs[0]} += $inobjs[4];
				}
			}
		}									 
	}	
}
close LIST;

foreach my $aa (@aas) { print $aa . "\t";}
print "\n";
foreach my $aa (@aas) {
	if (defined $helixhshisto{$aa} ) {
		print $helixhshisto{$aa} . "\t";
	} else {
		print "\t";
	}
}
print "\n";
foreach my $aa (@aas) {
	if (defined $helixhisto{$aa} ) {
		print $helixhisto{$aa} . "\t";
	} else {
		print "\t";
	}
}
print "\n"; 
foreach my $aa (@aas) { 
	if (defined $ccddgs{$aa} and defined $cchisto{$aa} ) {
		my $x = sprintf("%.4f", $ccddgs{$aa}/$cchisto{$aa}); print "$x\t";
	} else {
		print "\t";
	}
}
print "\n"; 
foreach my $aa (@aas) {
	if ( defined $cchisto{$aa} ) {
		print $cchisto{$aa} . "\t";
	} else {
		print "\t";
	}
}
print "\n"; 
foreach my $aa (@aas) {
	if ( defined $hsddgs{$aa} and defined $helixhshisto{$aa} ) {
		my $x = sprintf("%.4f", $hsddgs{$aa}/$helixhshisto{$aa}); print "$x\t";
	} else {
		print "\t";
	}
}
print "\n"; 
foreach my $aa (@aas) {
	if ( defined $ddgs{$aa} and defined $helixhisto{$aa} ) {
		my $x = sprintf("%.4f", $ddgs{$aa}/$helixhisto{$aa}); print "$x\t";
	} else {
		print "\t";
	}
}
print "\n"; 

